var plugin = requirePlugin("uploadPlugins");//引入插件
const qiniuUtil = require("./../../utils/qiniu.js");
const app = getApp();
import Toast from '@vant/weapp/toast/toast';
import Dialog from '/@vant/weapp/dialog/dialog';
import wxRequest from "../../utils/http.js";

Page({
    data: {
        qiniuToken: '',
        test: "plugin",
        textareaAValue: '',
        imageArray: [],
        attachments: [],
        icon: {//上传图片的icon
            width: "150rpx",//图片和图标的宽，选填
            height: "150rpx",//图片和图标的高，选填
            path: "",//选择图片icon的URL地址，必须是网络地址，不能是本地地址，如果不填则选默认图标,
            showImage: true//是否显示上传后的图片
        },
        qiniu: {
            uploadNumber: 9,//可以上传图片的数量限制,默认九张
            region: "SCN",//ECN, SCN, NCN, NA，您的七牛存储区域
            // token: token,//七牛上传token凭证
            token: '',
            domain: "http://images.yoonada.cn",//您配置的七牛CDN域名
            returnAllImage: true,//是否返回上传图片的数组,false上传一个返回一个
        },
        canPost: true
    },
    onLoad: function () {
        wx.setNavigationBarTitle({
            title: '发动态'
        })
        this.getQiNiuToken();
        if (wx.getStorageSync('token') === null || wx.getStorageSync('token') === '') {
            Dialog.alert({
                title: '温馨提示',
                message: '尚未授权登录，前往授权页面',
                theme: 'round-button',
            }).then(() => {
                wx.switchTab({
                    url: '../mine/mine'
                })
            });
        }
    },
    /**
     * 获取七牛token
     */
    getQiNiuToken: function () {
        qiniuUtil.getQiniuToken(res => {
            let qiniu = this.data.qiniu;
            qiniu.token = res;
            this.setData({qiniu: qiniu})
        })
    },

    textareaAInput(e) {
        this.setData({
            textareaAValue: e.detail.value
        })
    },
    /**
     * 上传成功后的回调,返回已上传的图片数组
     */
    uploadSuccess: function (uploadImage) {
        this.setData({imageArray: uploadImage.detail})
    },

    /**
     * 删除图片的回调，返回已上传的图片数组
     */
    deleteSuccess: function (uploadImage) {
        this.data.imageArray.splice(0, this.data.imageArray.length);
        this.data.attachments.splice(0, this.data.attachments.length);
        this.setData({
            imageArray: uploadImage.detail
        })
    },

    /**
     * 上传图片出错的回调
     */
    uploadError: function (res) {
        console.log("上传出错：" + res);
    },
    postDynamic() {
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
        let attachments = this.data.attachments;
        for (let i = 0; i < this.data.imageArray.length; i++) {
            attachments.push(this.data.imageArray[i].uploadResult.imageURL);
        }
        Toast.loading({
            mask: true,
            message: '拼命上传中...',
            duration: 0
        });
        wx.request({
            url: app.globalData.baseUrl + '/dynamic/publishDynamic',
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded',
                "token": wx.getStorageSync('token')
            },
            data: {
                dynamicContent: this.data.textareaAValue,
                pictureUrl: attachments,
                avatarUrl: wx.getStorageSync('avatarUrl'),
                nickName: wx.getStorageSync('nickName')
            },
            success: res => {
                if (res.data.code === 200) {
                    wx.showToast({
                        title: '发布成功',
                        icon: 'none'
                    });
                    prevPage.changeData();
                    wx.switchTab({
                        url: '../dynamic/dynamic'
                    })
                } else{
                    wx.showToast({
                        title: '发布失败,请联系客服',
                        icon: 'none'
                    });
                }
            }
        })

    }
})
